package com.qingyun.search.listener;


import com.alibaba.fastjson.JSON;

import com.qingyun.common.constants.VideoConstants;
import com.qingyun.model.search.vos.SearchVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;

@Component
@Slf4j
public class SyncVideoListener {

    @Resource
    private RestHighLevelClient restHighLevelClient;

    @KafkaListener(topics = VideoConstants.VIDEO_ES_SYNC_TOPIC)
    public void onMessage(String message){
        if (StringUtils.isNotBlank(message)){
            log.info("接收的消息为：",message);
            SearchVo searchVo = JSON.parseObject(message, SearchVo.class);
            IndexRequest indexRequest = new IndexRequest("info_video");
            indexRequest.id(searchVo.getId().toString());
            indexRequest.source(message, XContentType.JSON);
            try {
                restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
            } catch (IOException e) {
                e.printStackTrace();
                log.error("同步es错误",e);
            }
        }
    }
}
